package com.doubi.controller.sysPermission;


import com.doubi.common.config.shiro.ShiroUtil;
import com.doubi.model.sys.TSysPermission;
import com.doubi.model.sys.TSysUser;
import com.doubi.service.SysPermissionService;
import com.doubi.service.SysUserService;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: MrDoubi
 * Date: 2019/1/9
 * Time: 10:28
 */
@Controller
@RequestMapping("permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysUserService sysUserService;


    /**
     * MrDoubi
     * 2019年1月10日 15:04:57
     * 权限列表页面
     * @return 页面
     */
    @RequiresPermissions("permission:list")
    @GetMapping("list")
    public String list(){
        return "web/permission/list";
    }


    /**
     * MrDoubi
     * 2019年1月10日 15:05:23
     * 权限列表json
     * @return model
     */
    @RequiresPermissions("permission:json")
    @PostMapping("json")
    @ResponseBody
    public AppModel getList(){
        List<TSysPermission> permissions = permissionService.getAll();
        return new AppModel(0,"获取成功！",permissions);
    }


    /**
     * 功能说明：权限添加页面
     * @author  MrDoubi 2019-01-10 15:13:46
     * @param permission bean
     * @param model model
     * @return 页面
     */
    @RequiresPermissions("permission:add")
    @GetMapping("add")
    public String add(TSysPermission permission, Model model){
        model.addAttribute("bean",permission);
        return "web/permission/add";
    }


    /**
     * 功能说明：权限修改页面
     * @author  MrDoubi 2019-01-10 15:14:04
     * @param permission bean
     * @param model model
     * @return 页面
     */
    @RequiresPermissions("permission:edit")
    @GetMapping("edit")
    public String edit(TSysPermission permission, Model model){
        if(DataUtil.isNotEmpty(permission.getOid())){
            permission = permissionService.getById(permission.getOid());
        }
        model.addAttribute("bean",permission);
        return "web/permission/edit";
    }

    /**
     * 功能说明：权限添加/修改 接口
     * @author  MrDoubi 2019-01-10 15:14:33
     * @param permission bean
     * @return model
     */
    @RequiresPermissions("permission:saveOrUpdate")
    @PostMapping("saveOrUpdate")
    @ResponseBody
    public AppModel saveOrUpdate(TSysPermission permission){
        TSysUser sysUser = ShiroUtil.getUser();
        return permissionService.saveOrUpdate(permission,sysUser);
    }


    /**
     * 功能说明：权限删除
     * @author  MrDoubi 2019-01-10 15:14:47
     * @param permission 权限
     * @return model
     */
    @RequiresPermissions("permission:del")
    @PostMapping("del")
    @ResponseBody
    public AppModel del(TSysPermission permission){
        return permissionService.delBean(permission);
    }

    /**
     * 功能说明：角色编辑权限页面
     * @author  MrDoubi 2019-01-10 15:15:15
     * @param permission bean
     * @param model model
     * @return 页面
     */
    @RequiresPermissions("permission:editRole")
    @GetMapping("editRole")
    public String editRole(TSysPermission permission, Model model){
        model.addAttribute("bean",permission);
        return "web/permission/editRole";
    }


    /**
     * 功能说明：获取角色和权限关联
     * @author  MrDoubi 2019-01-10 15:15:49
     * @param permission bean
     * @return model
     */
    @RequiresPermissions("permission:editRoleJson")
    @PostMapping("editRoleJson")
    @ResponseBody
    public AppModel editRoleJson(TSysPermission permission){
        return permissionService.getAllByRole(permission.getOid());
    }

    /**
     * 功能说明：角色 权限关联提交
     * @author  MrDoubi 2019-01-10 15:16:13
     * @param oid 选择的全部权限
     * @param roleOid 角色oid
     * @return model
     */
    @RequiresPermissions("permission:editUserRole")
    @PostMapping("editUserRole")
    @ResponseBody
    public AppModel editUserRole(String[] oid,String roleOid){
        return permissionService.editUserRole(oid,roleOid);
    }
}
